package top.deepdesigner.controller.goods;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.*;
import top.deepdesigner.service.*;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CatAndBrandVO;
import top.deepdesigner.vo.CatVo;
import top.deepdesigner.vo.CommonListVO;
import top.deepdesigner.vo.GoodsAllinone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 11:02:08 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/goods")
public class GoodsController {

  @Autowired
  private DtsGoodsService goodsService;
  @Autowired
  private DtsGoodsAttributeService goodsAttributeService;
  @Autowired
  private DtsGoodsSpecificationService goodsSpecificationService;
  @Autowired
  private DtsGoodsProductService goodsProductService;
  @Autowired
  private DtsBrandService brandService;
  @Autowired
  private DtsCategoryService categoryService;

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品列表"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:goods:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params, @RequestParam(defaultValue = "") Integer goodsSn, @RequestParam(defaultValue = "") String name) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");
    CommonListVO<DtsGoods> goodsListVO = new CommonListVO<>();
    //获取分页数量
    Long count = goodsService.getGoodsCount();
    List<DtsGoods> goodsList = goodsService.getGoodsList(page, limit, sort, order, goodsSn, name);
    goodsListVO.setTotal(count);
    goodsListVO.setItems(goodsList);
    return ResponseUtil.ok(goodsListVO);
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品编辑"}, button = "获取详情")
  @RequiresPermissions(value = {"admin:goods:detail"})
  @GetMapping("/detail")
  public Object detail(@RequestParam(defaultValue = "") Long id) {
    GoodsAllinone<Integer> integerGoodsAllinone = new GoodsAllinone<>();
    //查询需要的值
    DtsGoods goodsByGoodId = goodsService.getGoodsByGoodId(id);
    List<Integer> goodsCategoryIds = goodsService.getGoodsCategoryByGoodId(id);
    List<DtsGoodsAttribute> goodsAttributes = goodsAttributeService.getGoodsAttributeByGoodId(id);
    List<DtsGoodsSpecification> goodsSpecifications = goodsSpecificationService.getGoodsSpecificationByGoodId(id);
    List<DtsGoodsProduct> goodsProducts = goodsProductService.getGoodsProductByGoodId(id);
    //整合返回对象
    integerGoodsAllinone.setCategoryIds(goodsCategoryIds);
    integerGoodsAllinone.setGoods(goodsByGoodId);
    integerGoodsAllinone.setAttributes(goodsAttributes);
    integerGoodsAllinone.setSpecifications(goodsSpecifications);
    integerGoodsAllinone.setProducts(goodsProducts);
    return ResponseUtil.ok(integerGoodsAllinone);
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品编辑"}, button = "获取分类和品牌")
  @RequiresPermissions(value = {"admin:goods:catAndBrand"})
  @GetMapping("/catAndBrand")
  public Object catAndBrand() {
    CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
    List<DtsBrand> brandList = brandService.getBrandList();
    List<DtsCategory> categoryList = categoryService.getCategoryList();
    //格式化品牌信息
    List<CatVo> collect = brandList.stream().map(item -> {
      CatVo catVo = new CatVo();
      catVo.setValue(item.getId());
      catVo.setLabel(item.getName());
      return catVo;
    }).collect(Collectors.toList());

    //格式化分类信息
    //顶级分类
    List<CatVo> topCategory = categoryList.stream().filter(item -> item.getPid() == 0).map(item -> {
      CatVo catVo = new CatVo();
      catVo.setValue(item.getId());
      catVo.setLabel(item.getName());
      return catVo;
    }).collect(Collectors.toList());

    //二级分类添加到顶级分类
    List<CatVo> includeSecCategory = topCategory.stream().peek(topItem -> {
      ArrayList<CatVo> catVos = new ArrayList<>();
      categoryList.forEach(item -> {
        if (Objects.equals(topItem.getValue(), item.getPid())) {
          CatVo catVo = new CatVo();
          catVo.setValue(item.getId());
          catVo.setLabel(item.getName());
          catVos.add(catVo);
        }
      });
      topItem.setChildren(catVos);
    }).collect(Collectors.toList());

    catAndBrandVO.setBrandList(collect);
    catAndBrandVO.setCategoryList(includeSecCategory);
    return ResponseUtil.ok(catAndBrandVO);
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品编辑"}, button = "编辑")
  @RequiresPermissions(value = {"admin:goods:update"})
  @PostMapping("/update")
  public Object update(@RequestBody GoodsAllinone<Integer> goodsAllinone) {
    System.out.println("goodsAllinone = " + goodsAllinone);
    goodsService.updateGoods(goodsAllinone);
    return ResponseUtil.ok();
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品上架"}, button = "添加")
  @RequiresPermissions(value = {"admin:goods:create"})
  @PostMapping("/create")
  public Object create(@RequestBody GoodsAllinone<Integer> goodsAllinone) {
    System.out.println("goodsAllinone = " + goodsAllinone);
    Boolean goods = goodsService.createGoods(goodsAllinone);
    if (goods) return ResponseUtil.ok();
    return ResponseUtil.fail(700, "商品id已存在");
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品列表"}, button = "删除")
  @RequiresPermissions(value = {"admin:goods:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsGoods goods) {
    System.out.println("goods = " + goods);
    Integer goodsId = goods.getId();
    goodsService.deleteGoods(goodsId);
    goodsProductService.deleteGoodsProduct(goodsId);
    goodsAttributeService.deleteGoodsAttribute(goodsId);
    goodsSpecificationService.deleteGoodsSpecification(goodsId);
    return ResponseUtil.ok();
  }
}
